package cn.aigoubuy.web.servelt;

import cn.aigoubuy.domain.Category;
import cn.aigoubuy.service.CategoryService;
import cn.aigoubuy.service.impl.CategoryServiceImpl;
import cn.aigoubuy.web.base.BaseServlet;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/categoryServlet")
public class CategoryServlet extends BaseServlet {
    public String findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // 1.1 查询所有的分类
        CategoryService categoryService = new CategoryServiceImpl();
        /*List<Category> allCategory = categoryService.findAll();
        // 1.2 将查询结果转为json
        String jsonStr = JSONObject.toJSONString(allCategory);*/
        String jsonStr = categoryService.findAllByAjax();
        // 1.3 将结果响应给浏览器
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().println(jsonStr);

        return null;
    }
}